/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZAuthResult;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Options;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.qa.unittest.prov.soap.SoapTest;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.net.HttpURLConnection;
/*     */ import java.net.URL;
/*     */ import java.util.Arrays;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSoap
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*  52 */   private static final String NAME_PREFIX = TestSoap.class.getSimpleName();
/*     */   
/*     */   private String mOriginalSoapRequestMaxSize;
/*     */   private String mOriginalSoapExposeVersion;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  60 */     Server server = Provisioning.getInstance().getLocalServer();
/*  61 */     this.mOriginalSoapRequestMaxSize = server.getAttr("zimbraSoapRequestMaxSize", "");
/*  62 */     this.mOriginalSoapExposeVersion = server.getAttr("zimbraSoapExposeVersion", "");
/*  63 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testSoapRequestMaxSize() throws Exception
/*     */   {
/*  68 */     StringBuilder messageBody = new StringBuilder();
/*  69 */     for (int i = 1; i <= 100; i++) {
/*  70 */       messageBody.append("Morey Amsterdam was a great man.  Morey Amsterdam was not a sandwich.\r\n");
/*     */     }
/*     */     
/*  73 */     setSoapRequestMaxSize(100000);
/*  74 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  75 */     TestUtil.sendMessage(mbox, "user1", NAME_PREFIX + " 1", messageBody.toString());
/*     */     
/*  77 */     setSoapRequestMaxSize(1000);
/*     */     try {
/*  79 */       TestUtil.sendMessage(mbox, "user1", NAME_PREFIX + " 2", messageBody.toString());
/*  80 */       fail("SOAP request should not have succeeded.");
/*     */     } catch (SoapFaultException e) {
/*  82 */       assertTrue("Unexpected error: " + e.toString(), e.toString().contains("bytes set for zimbraSoapRequestMaxSize"));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAccountGetInfoRequest()
/*     */     throws Exception
/*     */   {
/*  91 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/*  92 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.GET_VERSION_INFO_REQUEST);
/*     */     
/*     */ 
/*  95 */     TestUtil.setServerAttr("zimbraSoapExposeVersion", "TRUE");
/*  96 */     Element response = transport.invoke(request);
/*  97 */     validateSoapVersionResponse(response);
/*     */     
/*     */ 
/* 100 */     TestUtil.setServerAttr("zimbraSoapExposeVersion", "FALSE");
/* 101 */     request = Element.create(transport.getRequestProtocol(), AccountConstants.GET_VERSION_INFO_REQUEST);
/*     */     try {
/* 103 */       response = transport.invoke(request);
/* 104 */       fail("GetInfoRequest should have failed");
/*     */     } catch (SoapFaultException e) {
/* 106 */       assertEquals("service.PERM_DENIED", e.getCode());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAdminGetInfoRequest()
/*     */     throws Exception
/*     */   {
/* 115 */     SoapTransport transport = SoapTest.authAdmin("admin");
/* 116 */     Element request = Element.create(transport.getRequestProtocol(), AdminConstants.GET_VERSION_INFO_REQUEST);
/* 117 */     Element response = transport.invoke(request);
/* 118 */     validateSoapVersionResponse(response);
/*     */   }
/*     */   
/*     */   private void validateSoapVersionResponse(Element response) throws ServiceException
/*     */   {
/* 123 */     assertEquals(AccountConstants.GET_VERSION_INFO_RESPONSE.getName(), response.getName());
/*     */     
/* 125 */     Element info = response.getElement("info");
/* 126 */     assertNotNull(info.getAttribute("buildDate"));
/* 127 */     assertNotNull(info.getAttribute("host"));
/* 128 */     assertNotNull(info.getAttribute("release"));
/* 129 */     assertNotNull(info.getAttribute("version"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAuthRequest()
/*     */     throws Exception
/*     */   {
/* 138 */     ZMailbox.Options options = new ZMailbox.Options();
/* 139 */     options.setAccount("user1");
/* 140 */     options.setAccountBy(Key.AccountBy.name);
/* 141 */     options.setPassword("test123");
/* 142 */     options.setUri(TestUtil.getSoapUrl());
/* 143 */     ZMailbox mbox = runAuthTest(options);
/* 144 */     ZAuthToken authToken = mbox.getAuthToken();
/*     */     
/*     */ 
/* 147 */     options = new ZMailbox.Options();
/* 148 */     options.setAuthToken(authToken);
/* 149 */     options.setAuthAuthToken(true);
/* 150 */     options.setUri(TestUtil.getSoapUrl());
/* 151 */     runAuthTest(options);
/*     */   }
/*     */   
/*     */   private ZMailbox runAuthTest(ZMailbox.Options options) throws Exception
/*     */   {
/* 156 */     List<String> attrNames = Arrays.asList(new String[] { "zimbraFeatureImportExportFolderEnabled", "zimbraFeatureOutOfOfficeReplyEnabled" });
/*     */     
/*     */ 
/* 159 */     List<String> prefNames = Arrays.asList(new String[] { "zimbraPrefComposeFormat", "zimbraPrefAutoSaveDraftInterval" });
/*     */     
/*     */ 
/*     */ 
/* 163 */     options.setAttrs(attrNames);
/* 164 */     options.setPrefs(prefNames);
/* 165 */     ZMailbox mbox = ZMailbox.getMailbox(options);
/*     */     
/* 167 */     ZAuthResult auth = mbox.getAuthResult();
/* 168 */     Map<String, List<String>> attrs = auth.getAttrs();
/* 169 */     Map<String, List<String>> prefs = auth.getPrefs();
/*     */     
/* 171 */     assertEquals(attrNames.size(), attrs.size());
/* 172 */     assertEquals(prefNames.size(), prefs.size());
/*     */     
/* 174 */     for (String attrName : attrNames) {
/* 175 */       assertTrue(attrs.containsKey(attrName));
/*     */     }
/* 177 */     for (String prefName : prefNames) {
/* 178 */       assertTrue(prefs.containsKey(prefName));
/*     */     }
/* 180 */     return mbox;
/*     */   }
/*     */   
/*     */   public void testGetFolders() throws Exception
/*     */   {
/* 185 */     ZMailbox.Options options = new ZMailbox.Options();
/* 186 */     options.setAccount("user1");
/* 187 */     options.setAccountBy(Key.AccountBy.name);
/* 188 */     options.setPassword("test123");
/* 189 */     options.setUri(TestUtil.getSoapUrl());
/* 190 */     options.setNoSession(true);
/* 191 */     ZMailbox mbox = ZMailbox.getMailbox(options);
/*     */     
/* 193 */     ZFolder inbox = mbox.getFolderByPath("/Inbox");
/* 194 */     assertEquals("Inbox", inbox.getName());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private String doLowLevelRequest(URL url, String request)
/*     */     throws IOException
/*     */   {
/* 203 */     HttpURLConnection conn = (HttpURLConnection)url.openConnection();
/* 204 */     conn.setRequestProperty("content-type", "text/xml");
/* 205 */     conn.setRequestMethod("POST");
/* 206 */     conn.setDoInput(true);
/* 207 */     conn.setDoOutput(true);
/* 208 */     conn.getOutputStream().write(request.getBytes("UTF-8"));
/*     */     
/* 210 */     conn.connect();
/*     */     InputStream is;
/* 212 */     InputStream is; if (conn.getResponseCode() <= 400) {
/* 213 */       is = conn.getInputStream();
/*     */     } else {
/* 215 */       is = conn.getErrorStream();
/*     */     }
/* 217 */     BufferedReader in = new BufferedReader(new InputStreamReader(is));
/* 218 */     StringBuilder resp = new StringBuilder();
/*     */     String inputLine;
/* 220 */     while ((inputLine = in.readLine()) != null) {
/* 221 */       resp.append(inputLine);
/*     */     }
/* 223 */     in.close();
/* 224 */     return resp.toString();
/*     */   }
/*     */   
/*     */   public void testBadXmlReqWantJSResp() throws IOException
/*     */   {
/* 229 */     StringBuilder req = new StringBuilder();
/* 230 */     req.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">\n").append("<soap:Header>\n").append("<context xmlns=\"urn:zimbra\">\n").append("<format xmlns=\"\" type=\"js\"/>\n").append("</context>\n").append("</soap:Header>\n").append("<soap:Body>\n").append("<NoOpRequest session=<> xmlns=\"urn:zimbraMail\"/>\n").append("</soap:Body>\n").append("</soap:Envelope>\n");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 240 */     String responseString = doLowLevelRequest(new URL(TestUtil.getSoapUrl() + "/WibbleRequest"), req.toString());
/* 241 */     assertTrue("Response should be a JSON fault", responseString.startsWith("{\"Body\":{\"Fault\":{\"Code\":"));
/*     */   }
/*     */   
/*     */   public void testBadXmlReqWantXmlResp() throws IOException
/*     */   {
/* 246 */     StringBuilder req = new StringBuilder();
/* 247 */     req.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">\n").append("<soap:Header>\n").append("<context xmlns=\"urn:zimbra\">\n").append("<format xmlns=\"\" type=\"xml\"/>\n").append("</context>\n").append("</soap:Header>\n").append("<soap:Body>\n").append("<NoOpRequest session=<> xmlns=\"urn:zimbraMail\"/>\n").append("</soap:Body>\n").append("</soap:Envelope>\n");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 257 */     String responseString = doLowLevelRequest(new URL(TestUtil.getSoapUrl() + "/WibbleRequest"), req.toString());
/* 258 */     assertTrue("Response should be a SOAP 1.2 fault", responseString.startsWith("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Body><soap:Fault>"));
/*     */   }
/*     */   
/*     */   public void testBadSoap11XmlReq()
/*     */     throws IOException
/*     */   {
/* 264 */     StringBuilder req = new StringBuilder();
/* 265 */     req.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n").append("  SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n").append("<SOAO-ENV:Body>\n").append("<comment>Missing end tag for body</comment>").append("</SOAO-ENV:Envelope>\n");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 270 */     String responseString = doLowLevelRequest(new URL(TestUtil.getSoapUrl() + "/WibbleRequest"), req.toString());
/* 271 */     assertTrue(String.format("Response [%s] should be a SOAP 1.1 fault with faultstring 'Documentparse failed'", new Object[] { responseString }), responseString.startsWith("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Document parse failed"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 280 */     TestUtil.setServerAttr("zimbraSoapRequestMaxSize", this.mOriginalSoapRequestMaxSize);
/* 281 */     TestUtil.setServerAttr("zimbraSoapExposeVersion", this.mOriginalSoapExposeVersion);
/* 282 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 287 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   private void setSoapRequestMaxSize(int numBytes) throws Exception
/*     */   {
/* 292 */     TestUtil.setServerAttr("zimbraSoapRequestMaxSize", Integer.toString(numBytes));
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 297 */     TestUtil.cliSetup();
/* 298 */     TestUtil.runTest(TestSoap.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSoap.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */